package com.chookss.tools;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.chookss.HomeActivityss;
import com.chookss.HomeKnowledgeActivity;
import com.chookss.MyApp;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.StaticClass;
import com.chookss.base.Token;
import com.chookss.home.entity.NavigationEntity;
import com.chookss.home.entity.WatermarkEntity;
import com.chookss.mine.entity.AccountEntity;
import com.chookss.mine.entity.MineNavigationEntity;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoConfig;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.Watermark;
import com.chookss.wxapi.JustWeChatExis;
import com.chookss.wxapi.ShareConstants;
import com.google.gson.reflect.TypeToken;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.johnrambo.ktea.net.http.RequestData;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NavigationTools {
    public static String getAllUrl(Context context, String data) {
        String url = "";
        if (Urls.baseUrl.contains("dev_kmer")) {
            url = data + "token=" + Token.INSTANCE.getToken() + "&time=" + System.currentTimeMillis() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&addressType=1";
        } else {
            url = data + "token=" + Token.INSTANCE.getToken() + "&time=" + System.currentTimeMillis() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&addressType=0";
        }
        return url;
    }

    public static String getAllUrl2(Context context, String data) {
        String url = "";
        if (Urls.baseUrl.contains("dev_kmer")) {
            url = data + "token=" + Token.INSTANCE.getToken() + "&time=" + System.currentTimeMillis() + Utils.getStatusBarHeightDp(context) + "&addressType=1";
        } else {
            url = data + "token=" + Token.INSTANCE.getToken() + "&time=" + System.currentTimeMillis() + Utils.getStatusBarHeightDp(context) + "&addressType=0";
        }
        return url;
    }

    //登录保存用户数据
    public static void saveUserData(Context context, JSONObject data, String phone, String password) {
        JSONArray jsonArray = null;
        JSONObject tokenObject = null;
        try {
            MyApp.TokenStatus = false;
            jsonArray = data.getJSONArray("floor");
            tokenObject = data.getJSONObject("token");
            PermissionSpUtils.clearAll(context);
            if (jsonArray.length() > 1) {
                NavigationTools.setHomeBottomPermissions(context, jsonArray);
            }
            String employeeCode = data.getString("employeeCode");
            String username = data.getString("username");
            Token.INSTANCE.setToken(tokenObject.getString("token"));
            Token.INSTANCE.setVersionName(Utils.getVersionName(context));
            RequestData.INSTANCE.setToken(tokenObject.getString("token"));
            RequestData.INSTANCE.setVersionName(Utils.getVersionName(context));
            MyApp.setAppSetting(tokenObject.getString("token"));
            OkGoConfig.init(MyApp.appCtx, tokenObject.getString("token"),
                    Utils.getVersionName(context));
            new ShareUtils().putString(context, StaticClass.TOKEN, tokenObject.getString("token"));
            new ShareUtils().putString(context, StaticClass.VERSIONNAME, Utils.getVersionName(context));
            new ShareUtils().putString(context, StaticClass.EMPLOYEECODE, employeeCode);
            new ShareUtils().putString(context, StaticClass.THEME_CODE, data.getString("themeCode"));
            new ShareUtils().putString(context, StaticClass.COMPANYNAME, data.getString("companyName"));
            new ShareUtils().putString(context, StaticClass.COMPANYCODE, data.getString("companyCode"));
            new ShareUtils().putString(context, StaticClass.COMPANYLOGO, data.getString("companyLogo"));
            new ShareUtils().putString(context, StaticClass.OLD_LOGIN_COMPANYNAME, data.getString("companyName"));
            new ShareUtils().putString(context, StaticClass.OLD_LOGIN_USERNAME, username);
            //账户信息
            if (Utils.isNull(phone) && Utils.isNull(password)) {
                //账号切换或者推送吊起来 不做 用户账号的存储
            } else {
                //账号密码登录  启动页登录两者逻辑基本一样   验证码登录
                AccountEntity accountEntity = new AccountEntity();
                accountEntity.setCompanyName(data.getString("companyName"));
                accountEntity.setCompanyCode(data.getString("companyCode"));
                accountEntity.setEmployeeCode(data.getString("employeeCode"));
                accountEntity.setHeadPhotoThumbUrl(data.getString("headThumbUrl"));
                accountEntity.setNickName(data.getString("nickName"));
                accountEntity.setPassword(password);
                accountEntity.setPhone(phone);
                cacheData(context, accountEntity, phone, password, data.getString("companyName"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void cacheData(Context context, AccountEntity accountEntity, String phone, String password, String companyName) {
        List<AccountEntity> list = XJson.getAccountsList(context, phone, AccountEntity.class);
        if (list.size() == 0) {
            list.add(accountEntity);
            XJson.setAccountsList(context, phone, list);
        } else {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCompanyName().equals(companyName) && list.get(i).getPhone().equals(phone)) {
                    if (Utils.isNull(password)) {
                        //验证码登录
                        list.get(i).setPassword("");
                        XJson.setAccountsList(context, phone, list);
                        return;
                    } else {
                        if (!password.equals(list.get(i).getPassword())) {
                            //账户密码登录
                            list.get(i).setPassword(password);
                            XJson.setAccountsList(context, phone, list);
                        } else {
                            //启动页登录
                        }
                        return;
                    }
                }
                if (i == list.size() - 1) {
                    //最后一个了表示
                    list.add(accountEntity);
                    XJson.setAccountsList(context, phone, list);
                }
            }
        }

    }
    public static void openMiniProgram(Context context, String page) {
        if (!JustWeChatExis.isWeChatAppInstalled(context)) {
            MyToast.show("未安装微信");
            return;
        }
        String appId = ShareConstants.WX_APP_KEY; // 填应用AppId
        IWXAPI api = WXAPIFactory.createWXAPI(context, appId);
        WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
        req.userName = "gh_f2bd4d47ab46"; // 填小程序原始id
        req.path = page;                  //拉起小程序页面的可带参路径，不填默认拉起小程序首页
        //WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE   正式版
        //WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW   体验版  测试环境一般用的体验版
        //WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_TEST   测试版
        req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;
        api.sendReq(req);
    }
    //切换账号跳转首页
    public static void toChangeHome(Context context) {
        EventBus.getDefault().post(new MyEvent("HomeOver"));
        context.startActivity(new Intent(context, HomeActivityss.class));
        if ("T002".equals(new ShareUtils().getString(context, StaticClass.THEME_CODE, ""))) {
            context.startActivity(new Intent(context, HomeKnowledgeActivity.class));
        }
    }

    public static WatermarkEntity getWaterMark(Context context, String type) {
        WatermarkEntity watermarkEntity = null;
        List<WatermarkEntity> list = XJson.getWatermarkList(context, WatermarkEntity.class);
        for (int i = 0; i < list.size(); i++) {
            if (type.equals(list.get(i).getAppWatermarkSign())) {
                watermarkEntity = list.get(i);
                return watermarkEntity;
            }

        }
        return watermarkEntity;
    }

    public static void setWaterMark(Activity context, String type) {
        WatermarkEntity watermarkEntity = getWaterMark(context, type);
        if (watermarkEntity != null) {
            if ("1".equals(watermarkEntity.getStatusCd())) {
                Watermark.getInstance(context)
                        .setText(new ShareUtils().getString(context, StaticClass.USER_NICKNAME, "") + new ShareUtils().getString(context, StaticClass.APPACCOUNT, ""))
                        .setTextColor(0x1f000000)
                        .setCompanyName(watermarkEntity.getCompanyWatermarkName())
                        .setTextSize(Utils.sp2px(context, 18))
                        .setRotation(-45)
                        .show();
            }
        }
    }
    public static void setWaterMarkFull(Activity context, String type) {
        WatermarkEntity watermarkEntity = getWaterMark(context, type);
        if (watermarkEntity != null) {
            if ("1".equals(watermarkEntity.getStatusCd())) {
                Watermark.getInstance(context)
                        .setText(new ShareUtils().getString(context, StaticClass.USER_NICKNAME, "") + new ShareUtils().getString(context, StaticClass.APPACCOUNT, ""))
                        .setTextColor(0x1f000000)
                        .setCompanyName(watermarkEntity.getCompanyWatermarkName())
                        .setTextSize(Utils.sp2px(context, 18))
                        .setRotation(-45)
                        .show(true);
            }
        }
    }
    public static void saveUseRecord(Context context,String useType,String navigationCode) {
        //保存应用及工具使用记录接口
        Map<String,String> map=new HashMap<>();
        map.put("themeCode",new ShareUtils().getString(context,StaticClass.THEME_CODE,""));
        map.put("useType",useType);//使用类型(1应用 2工具)
        map.put("navigationCode",navigationCode);
        MyHttpRequest.getDefault().postRequest(new TypeToken<LzyResponse<String>>() {
        }.getType(), null, Urls.saveUseRecord, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) {
            }
            @Override
            public void onError(String errorMsg){
            }
        });
    }


    // 01公告 02 日志 03资料 04学习计划05考试 06 面试 07 通讯录 08  个人能力09 我的待办10 发布视频11 发布新闻12发布日志13题库14视频15溯源
//    16图文发布17图文18邀请注册19 小视频  20 面试评价21日报22周报23月报24知识库25发布图库26图库27考勤28请假29电子工牌30报销31物料32借款33补贴34计步器
    //35刻章  36巡检  37工资条  38离职  39晋升  40项目管理 41加班
    public static List<NavigationEntity> getNavigation(Context context,List<NavigationEntity> data, int type) {
        //type 0综合版本 1知识库版本
        List<NavigationEntity> lists = new ArrayList<>();
        PermissionSpUtils.setBoolean(context,new ShareUtils().getString(context, StaticClass.BOTTOM_1_CODE,""),true);
        PermissionSpUtils.setBoolean(context,new ShareUtils().getString(context, StaticClass.BOTTOM_3_CODE,""),true);
        lists.addAll(data);
        for (int i = 0; i < data.size(); i++) {
            NavigationEntity entity = data.get(i);
            PermissionSpUtils.setBoolean(context,entity.getNavigationCode(),true);
            if ("09".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            } else if ("10".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            } else if ("11".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            } else if ("12".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            } else if ("16".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            } else if ("18".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            } else if ("19".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            } else if ("20".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            }else if ("21".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            } else if ("22".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            } else if ("23".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            } else if ("25".equals(entity.getNavigationCode())) {
                lists.remove(entity);
            }
            EventBus.getDefault().post(new MyEvent("updateNavigation"));
        }
        if (!PermissionSpUtils.getBoolean(context,"10",false) && !PermissionSpUtils.getBoolean(context,"11",false) && !PermissionSpUtils.getBoolean(context,"12",false) && !PermissionSpUtils.getBoolean(context,"16",false) && !PermissionSpUtils.getBoolean(context,"18",false) && !PermissionSpUtils.getBoolean(context,"25",false)) {
            EventBus.getDefault().post(new MyEvent("PublishPermissions_No"));
        } else {
            EventBus.getDefault().post(new MyEvent("PublishPermissions_Yes"));
        }
        if(type==1){
            //知识库主题所用
            lists.clear();
            if (PermissionSpUtils.getBoolean(context,"19",false)) {
                NavigationEntity entity = new NavigationEntity();
                entity.setLocalUrl(R.drawable.icon_xvideo_60);
                entity.setNavigationCode("19");
                entity.setNavigationName("小视频");
                lists.add(entity);
            }
            if (PermissionSpUtils.getBoolean(context,"14",false)) {
                NavigationEntity entity2 = new NavigationEntity();
                entity2.setLocalUrl(R.drawable.icon_video_60);
                entity2.setNavigationCode("14");
                entity2.setNavigationName("视频");
                lists.add(entity2);
            }
            if (PermissionSpUtils.getBoolean(context,"17",false)) {
                NavigationEntity entity = new NavigationEntity();
                entity.setLocalUrl(R.drawable.icon_tuwen_60);
                entity.setNavigationCode("17");
                entity.setNavigationName("图文");
                lists.add(entity);
            }
            if (PermissionSpUtils.getBoolean(context,"03",false)) {
                NavigationEntity entity = new NavigationEntity();
                entity.setLocalUrl(R.drawable.icon_fold_60);
                entity.setNavigationCode("03");
                entity.setNavigationName("资料库");
                lists.add(entity);
            }
            if (PermissionSpUtils.getBoolean(context,"26",false)) {
                NavigationEntity entity = new NavigationEntity();
                entity.setLocalUrl(R.drawable.icon_gallery_60);
                entity.setNavigationCode("26");
                entity.setNavigationName("图库");
                lists.add(entity);
            }
        }
        return lists;
    }


    public static void setHomeBottomPermissions(Context context, JSONArray jsonArray) {
        try {
            new ShareUtils().putString(context, StaticClass.BOTTOM_1_CODE, jsonArray.getJSONObject(0).getString("navigationCode"));
            new ShareUtils().putString(context, StaticClass.BOTTOM_1_NAME, jsonArray.getJSONObject(0).getString("navigationName"));
            new ShareUtils().putString(context, StaticClass.BOTTOM_1_IMG, jsonArray.getJSONObject(0).getString("toolsImg"));
            new ShareUtils().putString(context, StaticClass.BOTTOM_3_CODE, jsonArray.getJSONObject(1).getString("navigationCode"));
            new ShareUtils().putString(context, StaticClass.BOTTOM_3_NAME, jsonArray.getJSONObject(1).getString("navigationName"));
            new ShareUtils().putString(context, StaticClass.BOTTOM_3_IMG, jsonArray.getJSONObject(1).getString("toolsImg"));

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    public static List<MineNavigationEntity> getMineKnowledge(Context context) {
        // 01公告 02 日志 03资料 04学习计划05考试 06 面试 07 通讯录 08  个人能力09 我的待办10 发布视频11 发布新闻12发布日志13题库14视频
        List<MineNavigationEntity> lists = new ArrayList<>();
        if (PermissionSpUtils.getBoolean(context,"10",false)) {
            MineNavigationEntity entity1 = new MineNavigationEntity();
            entity1.setLogo(R.drawable.wdzs_xsp);
            entity1.setTitle("小视频");
            lists.add(entity1);
            MineNavigationEntity entity2 = new MineNavigationEntity();
            entity2.setLogo(R.drawable.wdzs_sp);
            entity2.setTitle("视频");
            lists.add(entity2);
        }
        if (PermissionSpUtils.getBoolean(context,"12",false)) {
            MineNavigationEntity entity3 = new MineNavigationEntity();
            entity3.setLogo(R.drawable.wdzs_rz);
            entity3.setTitle("日志");
            lists.add(entity3);
        }
        if (PermissionSpUtils.getBoolean(context,"03",false)) {
            MineNavigationEntity entity4 = new MineNavigationEntity();
            entity4.setLogo(R.drawable.wdzs_zl);
            entity4.setTitle("资料");
            lists.add(entity4);
        }
        if (PermissionSpUtils.getBoolean(context,"10",false)) {
            MineNavigationEntity entity5 = new MineNavigationEntity();
            entity5.setLogo(R.drawable.wdzs_jh);
            entity5.setTitle("集合");
            lists.add(entity5);
        }
        MineNavigationEntity entity6 = new MineNavigationEntity();
        entity6.setLogo(R.drawable.wdzs_kc);
        entity6.setTitle("课程");
        lists.add(entity6);
        if (PermissionSpUtils.getBoolean(context,"11",false)) {
            MineNavigationEntity entity7 = new MineNavigationEntity();
            entity7.setLogo(R.drawable.wdzs_xinwen);
            entity7.setTitle("新闻");
            lists.add(entity7);
        }
        if (PermissionSpUtils.getBoolean(context,"17",false)) {
            MineNavigationEntity entity8 = new MineNavigationEntity();
            entity8.setLogo(R.drawable.iv_tuwen);
            entity8.setTitle("图文");
            lists.add(entity8);
        }
        if (PermissionSpUtils.getBoolean(context,"26",false)) {
            MineNavigationEntity entity9 = new MineNavigationEntity();
            entity9.setLogo(R.drawable.iv_gallery);
            entity9.setTitle("图库");
            lists.add(entity9);
        }
        return lists;
    }

    public static List<MineNavigationEntity> getMineLearn(Context context) {
        List<MineNavigationEntity> lists = new ArrayList<>();
        if (PermissionSpUtils.getBoolean(context,"04",false)) {
            MineNavigationEntity entity1 = new MineNavigationEntity();
            entity1.setLogo(R.drawable.wdxx_xxjh);
            entity1.setTitle("学习计划");
            lists.add(entity1);
        }
        if (PermissionSpUtils.getBoolean(context,"13",false)) {
            MineNavigationEntity entity2 = new MineNavigationEntity();
            entity2.setLogo(R.drawable.wdxx_ctb);
            entity2.setTitle("错题本");
            lists.add(entity2);
        }
        return lists;
    }

    public static List<MineNavigationEntity> getMineWork(Context context) {
        List<MineNavigationEntity> lists = new ArrayList<>();
        if (PermissionSpUtils.getBoolean(context,"09",false)) {
            MineNavigationEntity entity1 = new MineNavigationEntity();
            entity1.setLogo(R.drawable.gzt_sh);
            entity1.setTitle("审核");
            lists.add(entity1);
        }
        if (PermissionSpUtils.getBoolean(context,"12",false)) {
            MineNavigationEntity entity2 = new MineNavigationEntity();
            entity2.setLogo(R.drawable.gzt_xrz);
            entity2.setTitle("写日志");
            lists.add(entity2);
            MineNavigationEntity entity3 = new MineNavigationEntity();
            entity3.setLogo(R.drawable.gzt_xzb);
            entity3.setTitle("写周报");
            lists.add(entity3);
            MineNavigationEntity entity4 = new MineNavigationEntity();
            entity4.setLogo(R.drawable.gzt_xyb);
            entity4.setTitle("写月报");
            lists.add(entity4);
        }
        if (PermissionSpUtils.getBoolean(context,"11",false)) {
            MineNavigationEntity entity5 = new MineNavigationEntity();
            entity5.setLogo(R.drawable.gzt_fxw);
            entity5.setTitle("发新闻");
            lists.add(entity5);
        }
        if (PermissionSpUtils.getBoolean(context,"06",false)) {
            MineNavigationEntity entity6 = new MineNavigationEntity();
            entity6.setLogo(R.drawable.iv_interview_60);
            entity6.setTitle("面试评价");
            lists.add(entity6);
        }
        if (!"T002".equals(new ShareUtils().getString(context, StaticClass.THEME_CODE, ""))) {
            MineNavigationEntity entity7 = new MineNavigationEntity();
            entity7.setLogo(R.drawable.iv_rank_60);
            entity7.setTitle("排行榜");
            lists.add(entity7);
            MineNavigationEntity entity8 = new MineNavigationEntity();
            entity8.setLogo(R.drawable.iv_upload_60);
            entity8.setTitle("最近上传");
            lists.add(entity8);
            MineNavigationEntity entity9 = new MineNavigationEntity();
            entity9.setLogo(R.drawable.iv_history_60);
            entity9.setTitle("最近浏览");
            lists.add(entity9);
        }
        return lists;
    }
    public static  String getWorkFlowName(String type){
        String workFlowName="";
        int workFlowType=Utils.changeNumber(type);
        // 1-新闻 2-视频 3-补卡 4-请假 5-外勤 6-图文7报销8物料9借款10补贴11用章12维修13项目交付14离职 15晋升16 回款17 修改项目金额18入职19加班
        switch (workFlowType){
            case 1:
                workFlowName= "新闻";
                break;
            case 2:
                workFlowName=  "视频";
                break;
            case 3:
                workFlowName=  "补卡";
                break;
            case 4:
                workFlowName=  "请假";
                break;
            case 5:
                workFlowName=  "外勤";
                break;
            case 6:
                workFlowName=  "图文";
                break;
            case 7:
                workFlowName=  "报销";
                break;
            case 8:
                workFlowName=  "物品";
                break;
            case 9:
                workFlowName=  "借款";
                break;
            case 10:
                workFlowName=  "补贴";
                break;
            case 11:
                workFlowName=  "用章";
                break;
            case 12:
                workFlowName=  "维修";
                break;
            case 13:
                workFlowName=  "项目\n交付";
                break;
            case 14:
                workFlowName=  "离职";
                break;
            case 15:
                workFlowName=  "晋升";
                break;
            case 16:
                workFlowName=  "回款";
                break;
            case 17:
                workFlowName=  "项目\n金额";
                break;
            case 18:
                workFlowName=  "入职";
                break;
            case 19:
                workFlowName=  "加班";
                break;

        }
         return   workFlowName;
    }
}
